using System;
using FireStarterModeller.BO;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.Logic.Commands
{
    public class CreateClassCommand : ICommand
    {
        private readonly DMAssembly _assembly;
        private string _className;

        public CreateClassCommand(DMAssembly assembly, string className)
        {
            _assembly = assembly;
            _className = className;
        }
        
        public void Apply()
        {
           
                DMClass dmClass = _assembly.CreateClass(_className);
                try
                {
                dmClass.Save();
            } catch (Exception)
            {
                _assembly.Classes.Remove(dmClass);
                throw;
            }
        }

        public void Revert()
        {
            DMClass dmClass = _assembly.Classes.Find(info => info.ClassNameBO == _className);
            if (dmClass == null)
            {
                string message = string.Format("Error reverting change. Class with the name '{0}' not found to remove.", _className);
                throw new HabaneroDeveloperException(message, "");
            }
            dmClass.MarkForDelete();
            dmClass.Save();
        }
    }
}
